/*
 * SFX - Sound eFfectX (with on/off support)
 *
 * Copyright 2007 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

// this whole thing could be refactored now that I'm not using Loader

import flash.media.Sound;
import flash.display.MovieClip;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.net.URLRequest;
import lib3f.LCG;

class SFX
{

	public static var wallplacement : Bool = false;
	public static var gameover : Bool = false;
	public static var fortdeath : Bool = false;

	public static var towerplacement : Bool = false;
	public static var gotcastle : Bool = false;
	public static var castledeath : Bool = false;

	public static var creepdeath : Bool = false;
	
	public static var loadok : Bool = false;
	
	public static var sounds : Array<Sound> = new Array();

	public static var soundon : Bool = true;
	
	public static function getsound_lib(base : Sound)
	{
		sounds.push(base);
	}
	
	public static function loadfail(foo : flash.events.IOErrorEvent)
	{
		loadok = false;
	}
	
	public static function load()
	{
			
		getsound_lib(new SPlacement());
		getsound_lib(new SGameover());
		getsound_lib(new SDamage());
		getsound_lib(new SPlacetower());
		getsound_lib(new SGotcastle());
		getsound_lib(new SDeadcastle());
		getsound_lib(new SCreep1());
		getsound_lib(new SCreep2());
		getsound_lib(new SCreep3());
		
	}
	
	public static function start()
	{
		wallplacement = false;
		creepdeath = false;
		gameover = false;
		fortdeath = false;
		towerplacement = false;
		gotcastle = false;
	}
	
	public static function end()
	{
	}
	
	public static function up()
	{

		if (sounds.length==9)
			loadok=true;
	
		if (loadok && soundon)
		{
			try
			{
				if (wallplacement)
					sounds[0].play();
				if (gameover)
					sounds[1].play();

				if (fortdeath)
					sounds[2].play();
				if (towerplacement)
					sounds[3].play();
				if (gotcastle)
					sounds[4].play();
				if (castledeath)
					sounds[5].play();
				if (creepdeath)
				{
					var nextdeath = LCG.seed;
					sounds[6 + nextdeath%3].play();	
				}
			}
			catch (foo : Dynamic) {}
		
			wallplacement = false;
			creepdeath = false;
			gameover = false;
			castledeath = false;

			fortdeath = false;
			towerplacement = false;
			gotcastle = false;
		}
	}
	
}

class SPlacement extends Sound {}
class SGameover extends Sound {}
class SDamage extends Sound {}
class SPlacetower extends Sound {}
class SGotcastle extends Sound {}
class SDeadcastle extends Sound {}
class SCreep1 extends Sound {}
class SCreep2 extends Sound {}
class SCreep3 extends Sound {}
